package com.its.us.web.controller.sys;

import com.its.us.web.controller.sys.base.BaseWebPaginationCURDController;
import com.its.us.web.controller.sys.base.WebCRUDBaseController;
import com.its.us.web.entity.sys.Menu;
import com.its.us.web.entity.sys.Role;
import com.its.us.web.entity.sys.User;
import com.its.us.web.service.sys.MenuService;
import com.its.us.web.utils.Tree.TreeMenu;
import com.its.us.web.vo.sys.ResultBean;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: master
 * @Date: 2020/11/5 18:55
 * @Version: 1.0
 */
@Api(tags = "菜单")
//@RestController
@Controller
@RequestMapping("menu")
@Slf4j
public class MenuController extends BaseWebPaginationCURDController {

    @RequestMapping("init")
    @Override
    public String init() {
        return "menu/init";
    }

    @Resource
    private MenuService menuService;

    /**
     * 登录
     *
     * @param userName
     * @return
     */
    @ApiOperation(value = "左侧菜单获取", notes = "返回用户当前菜单权限数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "登录名", required = true, dataType = "String"),
    })
    @PostMapping("getCurrentMenu")
    @ResponseBody
    public ResultBean getCurrentMenu(@RequestParam("userName") String userName) {
        List<TreeMenu> menus;
        try {
            menus = menuService.getCurrentMenu(userName);
        } catch (Exception e) {
            log.error("获取菜单错误：", e);
            return new ResultBean(e.getCause());
        }
        return new ResultBean(menus);
    }

    /**
     * 菜单列表
     *
     * @return
     */
    @ApiOperation(value = "菜单获取全部", notes = "返回所有菜单权限数据", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("getAllMenu")
    @ResponseBody
    @PreAuthorize("hasPermission('/menu/init','sys:menu:list')")
    public ResultBean getAllMenu() {
        List<TreeMenu> menus;
        try {
            menus = menuService.getAllMenus();
        } catch (Exception e) {
            log.error("获取菜单错误：", e);
            return new ResultBean(e.getCause());
        }
        return new ResultBean(menus);
    }

    /**
     * 保存菜单
     *
     * @param menu
     * @return
     */
    @ApiOperation(value = "保存菜单", notes = "保存菜单", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuId", value = "菜单id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "parentId", value = "父菜单id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuName", value = "菜单名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "permission", value = "权限标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuUrl", value = "菜单地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuDes", value = "菜单描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isButton", value = "是否按钮", required = true, dataType = "String"),
            @ApiImplicitParam(name = "icons", value = "菜单icon", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'操作成功.'}"),
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'数据已存在.'}"),
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'数据异常.'}"),
            @ApiResponse(code = -99, message = "{code:-99,msg:'fail',data:'打印错误'}")
    })
    @PostMapping("save")
    @ResponseBody
    //@PreAuthorize("hasRole('admin')")
    @PreAuthorize("hasPermission('/menu/save','sys:menu:save')")
    public ResultBean save(@RequestBody Menu menu) {
        ResultBean resultBean;
        try {
            resultBean = menuService.saveMenu(menu);
        } catch (Exception e) {
            log.error("菜单保存错误：", e);
            return new ResultBean(e.getCause());
        }
        return resultBean;
    }

    /**
     * 修改菜单
     *
     * @param menu
     * @return
     */
    @ApiOperation(value = "修改菜单", notes = "修改菜单", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuId", value = "菜单id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "parentId", value = "父菜单id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuName", value = "菜单名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "permission", value = "权限标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuUrl", value = "菜单地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menuDes", value = "菜单描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isButton", value = "是否按钮", required = true, dataType = "String"),
            @ApiImplicitParam(name = "icons", value = "菜单icon", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'操作成功.'}"),
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'数据已存在.'}"),
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'数据异常.'}"),
            @ApiResponse(code = -99, message = "{code:-99,msg:'fail',data:'打印错误'}")
    })
    @PostMapping("update")
    @ResponseBody
    //@PreAuthorize("hasRole('admin')")
    @PreAuthorize("hasPermission('/menu/update','sys:menu:update')")
    public ResultBean update(@RequestBody Menu menu) {
        ResultBean resultBean;
        try {
            resultBean = menuService.updateMenu(menu);
        } catch (Exception e) {
            log.error("菜单修改出现错误：", e);
            return new ResultBean(e.getCause());
        }
        return resultBean;
    }


    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除", notes = "根据ids删除[可以是多个id]", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "标识id", required = true, dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:'操作成功.'}"),
            @ApiResponse(code = -99, message = "{code:-99,msg:'fail',data:'打印错误'}")
    })
    @PostMapping("delete")
    @ResponseBody
    //@PreAuthorize("hasRole('admin')")
    @PreAuthorize("hasPermission('/menu/delete','sys:menu:delete')")
    public ResultBean delete(@RequestParam("ids[]") List<String> ids) {
        ResultBean resultBean;
        try {
            resultBean = menuService.deleteByIds(ids);
        } catch (Exception e) {
            log.error("菜单删除错误：", e);
            return new ResultBean(e.getCause());
        }
        return resultBean;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "详情", notes = "根据id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识id", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "{code:0,msg:'success',data:object}"),
            @ApiResponse(code = -99, message = "{code:-99,msg:'fail',data:'打印错误'}")
    })
    @GetMapping("detail")
    @ResponseBody
    public ResultBean detail(@RequestParam("id") String id) {
        Menu menu;
        try {
            menu = menuService.findById(id);
        } catch (Exception e) {
            log.error("账户查询错误：", e);
            return new ResultBean(e.getCause());
        }
        return new ResultBean(menu);
    }


    /**
     * role-menu列表
     * 1 角色回显使用
     *
     * @return
     */
    @ApiOperation(value = "根据角色id获取菜单", notes = "返回查询角色菜单数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String"),
    })
    @GetMapping("getMenuByRoleId")
    @ResponseBody
    //@PreAuthorize("hasPermission('/role/getMenuByRoleId','sys:role:byRoleId')")
    public ResultBean getMenuByRoleId(@RequestParam("roleId") String roleId) {
        List<Menu> menus;
        try {
            menus = menuService.getMenuByRoleId(roleId);
        } catch (Exception e) {
            log.error("根据角色id获取菜单数据取错误：", e);
            return new ResultBean(e.getCause());
        }
        return new ResultBean(menus);
    }

}
